<?php

namespace app\index\controller;

use think\Controller;

use \payment\Weixin;

use app\index\model\Member;
use think\facade\Session;

class Login extends Controller
{

    public function index(){
        if (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') == true && !session('?openid')){
            $wx = new Weixin();
            $openid = $wx->GetOpenid('http://fengniaoyizhan.net/login.html');
            session('openid', $openid);
        }
        if(cache(session('openid'))){
            session('user', cache(session('openid')));
            redirect('/index');
        };
        // halt($_COOKIE);
        return view('/login');
    }

    public function check(){
        $phone = request()->post('phone');
        $pwd = request()->post('pwd');
        if(!empty($phone) && !empty($pwd)){
            $user = Member::where('memb_phone',$phone)->find();
            $result = [
                'msg' => '用户名或密码错误',
                'code' => '1',
            ];
            if(!empty($user)){
                if($user->memb_black) return json(mesg('-1', '账号被限制登录，请与管理员联系'));
                $isTure = password_verify($pwd, $user->memb_password);
                if($isTure){
                    session('user', [
                        'id'        => $user->memb_id,
                        'name'      => $user->memb_name,
                        'idCard'    => $user->memb_IDCard,
                        'image'     => $user->memb_headImage,
                        'phone'     => $user->memb_phone,
                    ]);
                    session('first', true);
                    if(session('?openid') && !cache(session('openid')))
                        cache(session('openid'), session('user'), 60 * 60 * 24 * 15);
                    $result = [
                        'msg' => '登陆成功',
                        'code' => 0,
                    ];
                }
            }    
            return json($result);
        }else{
            return view('/login');
        }
    }

    public function logout(){
        cache(session('openid', NULL));
        session(null);
        return redirect('/login');
    }

    public function forgetPwd(){
        return view('/pwd-update');
    }

    // 修改密码
    public function updatePwd(){
        $phone = request()->post('phone');
        $captcha = request()->post('captcha');
        $pwd = request()->post('new_pwd');
        if(!empty($captcha) && !empty($pwd)){
            if($captcha != session('captcha')){
                session('captcha', null);
                return json([
                    'code' => '0',
                    'msg'    => '验证码不正确',
                ]);
            }
            if(empty($phone)){
                $res = Member::where('memb_id', session('user.id'))->update(['memb_password'=>password_hash($pwd, PASSWORD_DEFAULT)]);
            }else{
                $res = Member::where('memb_phone', $phone)->update(['memb_password'=>password_hash($pwd, PASSWORD_DEFAULT)]);
            }
            if($res > 0) {
                return json([
                    'code' => '0',
                    'msg'    => '修改成功',
                ]);
            }else{
                return json([
                    'code' => '1',
                    'msg'    => '当前账户不存在,请注册',
                ]);
            }
        }
    }

    public function errorPage(){
        return view('/404');
    }

}
